<?php

class TrabajoCampoController extends AweController {

    /**
     * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
     * using two-column layout. See 'protected/views/layouts/column2.php'.
     */
    public $layout = '//layouts/column2';
    public $defaultAction = 'index';

    public function filters() {
        return array(
            array('CrugeAccessControlFilter'),
        );
    }

    public function actionIndex() {

        $model = new TrabajoCampo();
        $info = array();
        $formato = array();
        $verdatos = false;
        if (isset($_POST['TrabajoCampo']['cedula'])) {
            $model->cedula = $_POST['TrabajoCampo']['cedula'];
            $ficha = Ficha::model()->findByAttributes(array('cedula' => $model->cedula));

            if (!empty($ficha)) {
                $areas = $ficha->idCarrera->areas;
                $niveles = Ficha::model()->NivelesOptions;

                $count = 0;
                foreach ($areas as $area) {
                    $total = 0;
                    $id = $area->id;
                    foreach ($niveles as $nivel) {

                        if ($id == 4 || $id == 12 || $id == 19 || $id == 8 || $id == 16 || $id == 24) {//ahi hacer las consultas a las otras tablas
                            if ($id == 4 || $id == 12 || $id == 19) {//Escuela Gestur // guisas prtocololo
                                $resul = $model->infoNumHorasAreaNivelEscuelaGestur_Vinculacion($ficha->id, $nivel, "rep_eventosybanquetes t");
                            } else {//vinculacion consulta
                                $resul = $model->infoNumHorasAreaNivelEscuelaGestur_Vinculacion($ficha->id, $nivel, "rep_vinculacion t");
                            }
                        } else {
                            $resul = $model->infoNumHorasAreaNivel($ficha->id, $area->id, $nivel);
                        }
                        $info[$area->nombre][$nivel] = $resul ? $resul : 0;
                        $total+=$resul;

                        $count ++;
                    }
                    $info[$area->nombre]['Total'] = $total;
                    $info[$area->nombre]['Estado'] = $total == $area->num_horas ? 'Completo' : 'Pendiente';
                }

                $id = 1;
                foreach ($info as $key => $raw) {
                    $formato[] = array(
                        'id' => $id,
                        'Areas' => $key,
                        'I' => $raw['I'],
                        'II' => $raw['II'],
                        'III' => $raw['III'],
                        'IV' => $raw['IV'],
                        'V' => $raw['V'],
                        'VI' => $raw['VI'],
                        'VII' => $raw['VII'],
                        'VIII' => $raw['VIII'],
                        'Total' => $raw['Total'],
                        'Estado' => $raw['Estado'],
                    );
                    $id++;
                }
                $verdatos = true;
            } else {
            Yii::app()->user->setFlash('error', 'Numero de Cedula no Registrado.');
            }
        }
        $this->render("index", array('info' => $formato, 'model' => $model, 'verdatos'=>$verdatos));
    }

}
